package com.lxl.core;

import com.lxl.config.TaskStatus;
import com.lxl.message.MsgStatus;
import com.lxl.service.InterfaceMonitorService;
import org.apache.commons.lang.StringUtils;
import org.pentaho.di.core.exception.KettleException;

import javax.servlet.AsyncContext;
import javax.servlet.ServletResponse;
import java.io.File;
import java.io.IOException;

public class ShortTaskExecutor extends TaskExecutor {
    public final static String EXECUTOR_TYPE = "short";

    protected AsyncContext context;

    public ShortTaskExecutor(AsyncContext context){
        this.context = context;
        super.taskDataMeta = new TaskDataMeta(context.getRequest());
    }

    @Override
    public void run() {
        ServletResponse response = context.getResponse();
        String realName = EXECUTOR_TYPE + this.taskDataMeta.interfaceMonitor.getInterfaceCategory();
        this.taskDataMeta.interfaceMonitor.setInterfaceCategory(realName);

        try {
            if (taskDataMeta.verifyTaskEffectiveness()) {
                response.getWriter().write(taskDataMeta
                        .msgProvider.provideMessage(MsgStatus.REQUEST_ERROR, "Inspection failed.Please check URL or FILE."));
                this.taskDataMeta.interfaceMonitor.setStatus(TaskStatus.ERROR);
                this.taskDataMeta.interfaceMonitor.setNote("Inspection failed.");
                //InterfaceMonitorService.createInstance().insert(this.taskDataMeta.interfaceMonitor);
                InterfaceMonitorService.createInstance().asyncSaveLog(this.taskDataMeta.interfaceMonitor);
                context.complete();
                return;
            }

            TaskResultMeta resultMeta = runTask();

            if (resultMeta.getCode() > 0) {
                response.getWriter().write(taskDataMeta
                        .msgProvider.provideMessage(MsgStatus.INTERNAL_SERVER_ERROR, "TaskExecutor execution failed. Please check the trans."));
                this.taskDataMeta.interfaceMonitor.setStatus(TaskStatus.ERROR);
                this.taskDataMeta.interfaceMonitor.setNote("Error Code" + resultMeta.getCode());
            }else{
                //获得结果
                String result = resultMeta.getResult();
                this.taskDataMeta.interfaceMonitor.setOutputData(result);

                //判断是否有模板，有转到相应的模板类
                File templateFile = new File(taskDataMeta.templatePath);
                if (templateFile.exists()){
                    //采用模板
                    response.getWriter().write(taskDataMeta.msgProvider.provideMessage(MsgStatus.SUCCESS, result, templateFile));
                }else{
                    //采用默认
                    response.getWriter().write(taskDataMeta.msgProvider.provideMessage(MsgStatus.SUCCESS, result));
                }
            }

            //暂时只记录错误日志以及耗时超过100ms的日志
            if (StringUtils.equals(String.valueOf(TaskStatus.ERROR),String.valueOf(this.taskDataMeta.interfaceMonitor.getStatus()))
                    || this.taskDataMeta.interfaceMonitor.getTimeConsuming() >= 100L) {
                InterfaceMonitorService.createInstance().asyncSaveLog(this.taskDataMeta.interfaceMonitor);
            }
        } catch (IOException e){
            e.printStackTrace();
            this.taskDataMeta.interfaceMonitor.setStatus(TaskStatus.ERROR);
            this.taskDataMeta.interfaceMonitor.setNote(e.getMessage());
            this.taskDataMeta.interfaceMonitor.setTimeConsuming(0L);
            InterfaceMonitorService.createInstance().asyncSaveLog(this.taskDataMeta.interfaceMonitor);
        } catch (KettleException e){
            e.printStackTrace();
            this.taskDataMeta.interfaceMonitor.setStatus(TaskStatus.ERROR);
            this.taskDataMeta.interfaceMonitor.setNote(e.getMessage());
            this.taskDataMeta.interfaceMonitor.setTimeConsuming(0L);
            InterfaceMonitorService.createInstance().asyncSaveLog(this.taskDataMeta.interfaceMonitor);
        } finally {
            context.complete();
        }
    }
}
